package com.yunxi.service.dubboService.impl.sat;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.model.sat.PartnerCustomerHis;
import com.yunxi.sat.pojo.PartnerCustomerHisVO;
import com.yunxi.sat.service.PartnerCustomerHisDubboService;
import com.yunxi.service.localService.sat.PartnerCustomerHisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 渠道客户关系历史 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-07-12
 */
public class PartnerCustomerHisDubboServiceImpl implements PartnerCustomerHisDubboService {

    @Autowired
    @Qualifier(value = "partnerCustomerHisServiceImpl")
    PartnerCustomerHisService partnerCustomerHisService;


    @Override
    public PartnerCustomerHisVO getById(String id) throws Exception {
        PartnerCustomerHis partnerCustomerHis = partnerCustomerHisService.getById(id);
        if (partnerCustomerHis != null) {
            PartnerCustomerHisVO partnerCustomerHisVO = new PartnerCustomerHisVO();
            BeanUtils.copyProperties(partnerCustomerHis, partnerCustomerHisVO);
            return partnerCustomerHisVO;
        } else {
            throw new Exception("操作失败,无法根据id:" + id + "找到对应记录!");
        }
    }

    @Override
    public void saveOrUpdate(PartnerCustomerHisVO partnerCustomerHisVO, LoginInfo loginInfo) throws Exception {
        setPartnerCode(partnerCustomerHisVO, loginInfo);
        if(StringUtils.isEmpty(partnerCustomerHisVO.getHisId())){
            partnerCustomerHisVO.setCreateBy(loginInfo.getUserCode());
        }else {
            partnerCustomerHisVO.setUpdateBy(loginInfo.getUserCode());
        }
        PartnerCustomerHis partnerCustomerHis = new PartnerCustomerHis();
        BeanUtils.copyProperties(partnerCustomerHisVO, partnerCustomerHis);
        partnerCustomerHisService.saveOrUpdate(partnerCustomerHis);
    }

    @Override
    public void removeById(String id) {
        partnerCustomerHisService.removeById(id);
    }

    @Override
    public PageInfo<PartnerCustomerHisVO> findPageByVO(PartnerCustomerHisVO partnerCustomerHisVO, Integer pageNum, Integer pageSize, LoginInfo loginInfo) throws Exception {
        setPartnerCode(partnerCustomerHisVO, loginInfo);
        PageInfo<PartnerCustomerHisVO> pageInfo = partnerCustomerHisService.pageByVo(partnerCustomerHisVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<PartnerCustomerHisVO> partnerCustomerHisVOList) throws Exception {
        partnerCustomerHisService.removeByListVo(partnerCustomerHisVOList);
    }

    @Override
    public List listByVo(PartnerCustomerHisVO partnerCustomerHisVO) {
        return partnerCustomerHisService.listByVo(partnerCustomerHisVO);
    }

    @Override
    public void del(List<String> list, String updateBy) throws Exception {
        if (list != null && list.size() > 0) {
            for (String id : list) {
                PartnerCustomerHis byId = partnerCustomerHisService.getById(id);
                if (byId != null) {
                    byId.setIsDel(1);
                    byId.setUpdateBy(updateBy);
                    partnerCustomerHisService.saveOrUpdate(byId);
                } else {
                    throw new Exception("操作失败,无法根据id:找到对应记录!");
                }
            }
        } else {
            throw new Exception("操作失败,list为空!");
        }
    }

    /**
     * 设置渠道商代码
     *
     * @param partnerCustomerHisVO
     * @param loginInfo
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void setPartnerCode(PartnerCustomerHisVO partnerCustomerHisVO, LoginInfo loginInfo) throws IllegalAccessException, InvocationTargetException {
        Object partner = loginInfo.getPartner();
        if (partner != null) {
            PartnerVO partnerVO = new PartnerVO();
            org.apache.commons.beanutils.BeanUtils.copyProperties(partnerVO, partner);
            partnerCustomerHisVO.setPartnerCode(partnerVO.getPartnerCode());
        }
    }
}
